﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using PocketMahjong.Core;
using System.Diagnostics;
using System.Reflection;

namespace PocketMahjong
{
	public partial class MainForm : Form
	{
		BoardView _view;
		private ImageList _images;

		public MainForm()
		{
			InitializeComponent();
			CreateMenu();

			_view = new BoardView();
			_view.Dock = DockStyle.Fill;
			_view.Parent = this;
			_view.Controller.StartNewGame();
		}

		private void CreateMenu()
		{
			_images = new ImageList();
			_images.ImageSize = new Size(32, 32);
			_toolBar.ImageList = _images;

			AddButton("Exit", Properties.Resources.cancel);
			AddButton("Tools", Properties.Resources.tools);
			AddButton(null, null);
			AddButton("New", Properties.Resources._new);
			AddButton("Restart", Properties.Resources.restart);
			AddButton(null, null);
			AddButton("Undo", Properties.Resources.undo);
			AddButton("Hint", Properties.Resources.find);
			AddButton(null, null);
			AddButton("Help", Properties.Resources.help);
		}

		private void AddButton(string tag, Bitmap bitmap)
		{
			var button = new ToolBarButton();
			if (tag != null)
			{
				button.Tag = tag;
				button.ImageIndex = _images.Images.Count;
				_images.Images.Add(bitmap);
			}
			else
				button.Style = ToolBarButtonStyle.Separator;
			_toolBar.Buttons.Add(button);
		}

		private void ExitClick()
		{
			//var res = MessageBox.Show("Exit game?", Text, MessageBoxButtons.OKCancel,
			//	MessageBoxIcon.None, MessageBoxDefaultButton.Button2);
			//if (res == DialogResult.OK)
			this.Close();
		}

		private void NewClick()
		{
			//var res = MessageBox.Show("Start new game?", Text, MessageBoxButtons.OKCancel, 
			//	MessageBoxIcon.None, MessageBoxDefaultButton.Button2);
			//if (res == DialogResult.OK)
			_view.Controller.StartNewGame();
		}

		private void RestartClick()
		{
			//var res = MessageBox.Show("Restart current game?", Text, MessageBoxButtons.OKCancel,
			//	MessageBoxIcon.None, MessageBoxDefaultButton.Button2);
			//if (res == DialogResult.OK)
			_view.Controller.Restart();
		}

		private void UndoClick()
		{
			_view.Controller.Undo();
		}

		private void HintClick()
		{
			_view.Controller.ShowHint();
		}

		private void ToolsClick()
		{
			var lay = Config.Instance.Layout;
			var ar = Config.Instance.IsRandom;
			var form = new ConfigForm();
			form.ShowDialog();
			if (lay != Config.Instance.Layout || ar != Config.Instance.IsRandom)
				_view.Controller.StartNewGame();
		}

		private void HelpClick()
		{
			var form = new AboutForm();
			form.ShowDialog();
		}

		private void _toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
		{
			string name = (e.Button.Tag as string) + "Click";
			var mi = this.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
			if (mi != null)
				mi.Invoke(this, null);
		}
	}
}